package com.industrial.app.controller.formal;

import cn.hutool.core.date.DateException;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import com.industrial.app.domain.formal.BizIndustrialSectorMetricsFormal;
import com.industrial.app.domain.request.BizIndustrialSectorRequest;
import com.industrial.app.service.IBizIndustrialSectorMetricsFormalService;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.web.page.TableDataInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 分行业企业主要经济指标正式Controller
 *
 * @author SangGuangCan
 * @date 2024-01-02
 */
@RestController
@RequestMapping("/biz/formal/metrics")
public class BizIndustrialSectorMetricsFormalController extends BaseController
{
    private final IBizIndustrialSectorMetricsFormalService bizIndustrialSectorMetricsFormalService;

    @Autowired
    public BizIndustrialSectorMetricsFormalController(IBizIndustrialSectorMetricsFormalService bizIndustrialSectorMetricsFormalService){
        this.bizIndustrialSectorMetricsFormalService = bizIndustrialSectorMetricsFormalService;
    }

    /**
     * 查询分行业企业主要经济指标正式列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BizIndustrialSectorMetricsFormal bizIndustrialSectorMetricsFormal)
    {
        String dates = bizIndustrialSectorMetricsFormal.getDates();
        if(StringUtils.isNotBlank(dates)){
            DateTime datesTimes= DateUtil.parse(bizIndustrialSectorMetricsFormal.getDates(),"yyyy年MM月");
            bizIndustrialSectorMetricsFormal.setYearMonth(datesTimes);
        }else{
            throw new RuntimeException("未找到日期");
        }
        startPage();
        List<BizIndustrialSectorMetricsFormal> list = bizIndustrialSectorMetricsFormalService.selectBizIndustrialSectorMetricsFormalList(bizIndustrialSectorMetricsFormal);
        return getDataTable(list);
    }

    /**
     * 导出分行业企业主要经济指标正式列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:formal:export')")
//    @Log(title = "分行业企业主要经济指标正式", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizIndustrialSectorMetricsFormal bizIndustrialSectorMetricsFormal)
    {
        List<BizIndustrialSectorMetricsFormal> list = bizIndustrialSectorMetricsFormalService.selectBizIndustrialSectorMetricsFormalList(bizIndustrialSectorMetricsFormal);
        ExcelUtil<BizIndustrialSectorMetricsFormal> util = new ExcelUtil<BizIndustrialSectorMetricsFormal>(BizIndustrialSectorMetricsFormal.class);
        util.exportExcel(response, list, "分行业企业主要经济指标正式数据");
    }

    /**
     * 获取分行业企业主要经济指标正式详细信息
     */
//    @PreAuthorize("@ss.hasPermi('biz:formal:query')")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(bizIndustrialSectorMetricsFormalService.selectBizIndustrialSectorMetricsFormalByBaseId(baseId));
    }

    /**
     * 新增分行业企业主要经济指标正式
     */
//    @PreAuthorize("@ss.hasPermi('biz:formal:add')")
//    @Log(title = "分行业企业主要经济指标正式", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizIndustrialSectorMetricsFormal bizIndustrialSectorMetricsFormal)
    {
        return toAjax(bizIndustrialSectorMetricsFormalService.insertBizIndustrialSectorMetricsFormal(bizIndustrialSectorMetricsFormal));
    }

    /**
     * 修改分行业企业主要经济指标正式
     */
//    @PreAuthorize("@ss.hasPermi('biz:formal:edit')")
//    @Log(title = "分行业企业主要经济指标正式", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizIndustrialSectorMetricsFormal bizIndustrialSectorMetricsFormal)
    {
        return toAjax(bizIndustrialSectorMetricsFormalService.updateBizIndustrialSectorMetricsFormal(bizIndustrialSectorMetricsFormal));
    }

    /**
     * 删除分行业企业主要经济指标正式
     */
//    @PreAuthorize("@ss.hasPermi('biz:formal:remove')")
//    @Log(title = "分行业企业主要经济指标正式", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(bizIndustrialSectorMetricsFormalService.deleteBizIndustrialSectorMetricsFormalByBaseIds(baseIds));
    }

    @GetMapping("/getUniqueDatesFromDatabase")
    public AjaxResult getUniqueDatesFromDatabase(){
        List<String> dateArgs = bizIndustrialSectorMetricsFormalService.getUniqueDatesFromDatabase();
        return AjaxResult.success(dateArgs);
    }

    @GetMapping("/getAppTableData")
    public AjaxResult getAppTableData(BizIndustrialSectorRequest request){
        try{
            String dates = request.getDates();
            if(StringUtils.isBlank(dates)){
                return AjaxResult.warn("没有找到日期");
            }
            DateUtil.parse(request.getDates(),"yyyy年MM月");
            return AjaxResult.success(this.bizIndustrialSectorMetricsFormalService.getAppTableData(request));
        }catch (DateException de){
            logger.error("日期格式校验失败,传入日期:{},正确格式:{}",request.getDates(),"yyyy年MM月");
            return AjaxResult.error("日期校验失败");
        }catch (Exception e){
            logger.error("未知错误:{}",e.getMessage());
            return AjaxResult.error(e.getMessage());
        }
    }
}
